#!/bin/bash
set -e

# fixme: read version.txt and paste to debian control file!
version=`cat version.txt`
arch=`awk '$1 ~ /Architecture/ { print $2 }' debian/DEBIAN/control`
debroot="dangerdeep_$version""_$arch"
debfile="dangerdeep_$version""_$arch.deb"
bindir='usr/games'
datadir='usr/share/games'
mandir='usr/share/man/man6'
docdir='usr/share/doc/dangerdeep'

mkdir -p $debroot
cp -R debian/* $debroot
#echo "Version: " $version >> $debroot/DEBIAN/control
for dir in DEBIAN $bindir $datadir/dangerdeep $mandir $docdir; do
	mkdir -p $debroot/$dir
done

# to compile and install it
scons installbindir=\#/$debroot/$bindir datadir=/$datadir/dangerdeep version=$version install usex86sse=1 -j3

# remove debug symbols from the binary
strip -R .comment -x -s $debroot/$bindir/dangerdeep

cp -f doc/man/*.[0-9] $debroot/$mandir
cp -f ChangeLog $debroot/$docdir/changelog
cp -fr doc/debian/[a-z]* $debroot/$docdir
rm -rf `find $debroot -name .sconsign`
rm -rf `find $debroot -name CVS`
rm -rf `find $debroot -name "SCons*"`
gzip -f --best $debroot/$mandir/*.[0-9]
gzip -f --best $debroot/$docdir/changelog
gzip -f --best $debroot/$docdir/changelog.Debian

find $debroot -type f -exec chmod 0644 \{\} \; -type d -exec chmod 0755 \{\} \;
chmod 0755 $debroot/$bindir/dangerdeep

# Is this really needed? The 'chmod' lines above should remove the setuid bit for all files and directories
chmod -R a-s $debroot

fakeroot dpkg-deb --build $debroot && \
	lintian $debfile && \
	echo "$debfile created"
rm -r $debroot
